import { inject } from '@angular/core/testing';
import { Component, OnInit , Injector} from '@angular/core';


import {AppComponentBase} from '@shared/app-component-base';


import { UploadDataComponent } from './../map/upload-data.component';
import { UploadModelComponent } from './upload-model.component';

import { WaterSamplingServiceProxy , SamplingPointDto , UploadWaterDataDto} from '@shared/service-proxies/service-proxies';

@Component({
  selector: 'app-water-data',
  templateUrl: './water-data.component.html',
  styles: []
})
export class WaterDataComponent extends AppComponentBase  implements OnInit {

    // self:any;
  constructor(injector: Injector,
        public waterSamplingService: WaterSamplingServiceProxy) {
      super(injector);

     // this.self = this;
   }

   dataTable = {
    pageIndex: 1,
    pageSize: 10,
    total: 200,
    list: []
};

  ngOnInit() {
    this.load();
  }

  upload() {

    this.showModal(UploadModelComponent,{
        title:'采样数据',
        action: this.uploadData,
        host:this
    }, () => {

         this.load();
    });

    }

    uploadData(file, host, riverType, excelType) {
        const dto = new UploadWaterDataDto();
        dto.file.fileName = file.fileName;
        dto.file.fileType = file.fileType;
        dto.file.fileToken = file.fileToken;
        dto.riverType = riverType;
        dto.excelFileType = excelType;
        dto.isRepeat = false;

       host.waterSamplingService.uploadData(dto).subscribe(result => {        
       console.log(result);
       if (result.isRepeat) {
        this.message.confirm(
            this.l('有重复数据，是否覆盖？'),
            isConfirmed => {
                if (isConfirmed) {
                    console.log(dto);
                    dto.isRepeat = true;
                    host.waterSamplingService.uploadData(dto).subscribe(() => {
                       
                            this.notify.success('覆盖成功');
                    });
                }
            }
        );
       
       } else {
        this.notify.success('导入成功');
    }
        
        });

    }


    load(pi?: number) {
        if (typeof pi !== 'undefined') {
            this.dataTable.pageIndex = pi || 1;
        }

        this.getDatas();
    }

    getDatas(){

        this.loading = true;
        this.waterSamplingService.getPoints(
            '',
            '',
            this.dataTable.pageSize,
            (this.dataTable.pageIndex - 1) * this.dataTable.pageSize)
            .finally(()=>{
                this.loading =false;
            })
            .subscribe((res)=>{
                console.log(res);
                this.dataTable.list = res.items;
                this.dataTable.total = res.totalCount;
            });

    }


}
